package com.example.content_provider_client;

import android.content.Intent;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.content_provider_client.utils.ToastHelper;

public class SendMms extends AppCompatActivity implements View.OnClickListener {

    private ActivityResultLauncher<Intent> resultLauncher;
    private ImageView evImage;
    private EditText etPhone;
    private EditText etTitle;
    private EditText etContent;
    private Uri imgUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_mms);

        etPhone = findViewById(R.id.et_phone);
        etTitle = findViewById(R.id.et_title);
        etContent = findViewById(R.id.et_content);

        evImage = findViewById(R.id.iv_add);
        Button etSend = findViewById(R.id.btn_send);
        evImage.setOnClickListener(this);
        etSend.setOnClickListener(this);

        // 跳转到相册，选择图片，并返回图片
        resultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                // 选择图片后，拿到选择的结果
                if (result.getResultCode() == RESULT_OK) {
                    Intent intent = result.getData();
                    // 选择的图片uri
                    // imgUri: content://media/picker_get_content/0/com.android.providers.media.photopicker/media/1000000034
                    imgUri = intent.getData();
                    if (imgUri != null) {
                        // 显示图片
                        evImage.setImageURI(imgUri);
                        Log.d("zhoutao", "imgUri: " + imgUri.toString());
                    }
                }
            }
        });

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        // 添加图片
        if (id == R.id.iv_add) {
            // 跳转到相册，选择图片，并返回图片
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            // 设置返回的文件类型为图片
            intent.setType("image/*");
            // 打开相册，等待图片选择结果
            resultLauncher.launch(intent);

        } else if (id == R.id.btn_send) {
            // 发送彩信，开始一个Activity
            Intent intent = new Intent(Intent.ACTION_SEND);
            // 开启新的任务栈
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // Intent 的接收者，允许读取intent携带的URI数据
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            // 彩信发送的目标号码
            intent.putExtra("address", etPhone.getText().toString());
            // 彩信发送的标题
            intent.putExtra("subject", etTitle.getText().toString());
            // 彩信发送的内容
            intent.putExtra("sms_body", etContent.getText().toString());
            // 彩信发送的附件
            intent.putExtra(Intent.EXTRA_STREAM, imgUri);
            // 设置附件类型
            intent.setType("image/*");
            // 由于未指定打开哪个页面，所以系统会弹出选择窗口
            startActivity(intent);
            ToastHelper.show(this, "请选择短信的方式发送彩信");

        }
    }
}